package com.goodving.guli.bt.weight;

import android.content.Context;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.ad;
import com.goodving.guli.bt.ae;
import com.goodving.guli.bt.model.WeightGoal;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.view.C3657a;

import java.util.List;

/**
 * Created by john on 2015/12/4.
 */
public class WeightGoalManager { // C2032a
    private static final String f11511a = "WeightGoalManager";
    private static WeightGoalManager f11512b;

    public static WeightGoalManager getInstance() { // m15884a
        if (f11512b == null) {
            f11512b = new WeightGoalManager();
        }
        return f11512b;
    }

    class C2033b implements ad {
        final /* synthetic */ WeightGoalManager f11513a;

        C2033b(WeightGoalManager c2032a) {
            this.f11513a = c2032a;
        }

        public void m8237a() {
            GuliLog.tracePrintInfo("WeightGoalManager", "\u670d\u52a1\u5668\u540c\u6b65\u5931\u8d25");
        }

        public void m8238a(WeightGoal weightGoal) {
            GuliLog.tracePrintInfo("WeightGoalManager", "\u670d\u52a1\u5668\u66f4\u65b0\u6210\u529f\uff0c\u672c\u5730\u72b6\u6001\u4f4d\u7f6e\u4e3aSYNCED");
            weightGoal.synced = WeightGoal.WEIGHTGOAL_SYNCED;
            weightGoal.save();
        }
    }

    private void m15885a(Context context, WeightGoal weightGoal) {
        Utils.m8000a(context, weightGoal, new C2033b(this));
    }

    private void m15886b(Context context, WeightGoal weightGoal) {
        GuliLog.tracePrintInfo(f11511a, " local doesnt found data and can not del");
//        Utils.m8003a(context, weightGoal.fuid + C3657a.f18328f, weightGoal.datetime, new C2034c(this, weightGoal));
    }

    public WeightGoal m15887a(int i, long j) {
        Object[] objArr = new Object[]{Long.valueOf(j)};
        objArr = new Object[]{Integer.valueOf(WeightGoal.WEIGHTGOAL_NEED_DELETE)};
        List d = new Select().from(WeightGoal.class).where("FUID=?", Integer.valueOf(i)).and("DateTime <= ?", objArr).and("Synced!=?", objArr).orderBy("DateTime DESC").execute();
        if (d == null || d.size() <= 0) {
            GuliLog.tracePrintInfo(f11511a, "size null");
            return null;
        }
        GuliLog.tracePrintInfo(f11511a, "size " + d.size());
        return (WeightGoal) d.get(0);
    }

    public List<WeightGoal> m15888a(int i, int i2) {
        GuliLog.tracePrintInfo(f11511a, "get goal list by uid " + i + " tag " + i2);
        return new Select().from(WeightGoal.class).where("FUID=?", Integer.valueOf(i)).and("Synced=?", Integer.valueOf(i2)).execute();
    }

    public void syncLocalToServer(Context context) { // m15889a
        GuliLog.tracePrintInfo(f11511a, "syncLocalToServer...");
        for (UserInfo userInfo : UserInfoManager.getInstance().m15598d()) {
            int i = userInfo.uid;
            List<WeightGoal> a = m15888a(i, WeightGoal.WEIGHTGOAL_NEED_DELETE);
            GuliLog.tracePrintInfo(f11511a, a == null ? "needdelelist is empty " : " need delete " + a.size() + " weight goal");
            for (WeightGoal weightGoal : a) {
                GuliLog.tracePrintInfo(f11511a, "dele goal");
                m15886b(context, weightGoal);
            }
            List<WeightGoal> a2 = m15888a(i, WeightGoal.WEIGHTGOAL_NEED_SYNC);
            GuliLog.tracePrintInfo(f11511a, a2 == null ? "needUpdateList is empty " : " need update " + a2.size() + " weight goal");
            for (WeightGoal weightGoal2 : a2) {
                GuliLog.tracePrintInfo(f11511a, "udpate goal");
                m15885a(context, weightGoal2);
            }
        }
    }

    class C2035d implements ae {
        final /* synthetic */ WeightGoalManager f11516a;

        C2035d(WeightGoalManager c2032a) {
            this.f11516a = c2032a;
        }

        public void m8239a(List<WeightGoal> list) {
            if (list != null) {
                for (WeightGoal weightGoal : list) {
                    weightGoal.synced = WeightGoal.WEIGHTGOAL_SYNCED;
                    weightGoal.save();
                }
            }
        }
    }

    public void syncFromServer(Context context, List<UserInfo> list) { // m15890a
        GuliLog.tracePrintInfo(f11511a, "syncFromServer...");
        for (UserInfo userInfo : list) {
            Utils.m8005a(context, userInfo.uid + C3657a.f18328f, C3657a.f18328f, C3657a.f18328f, new C2035d(this));
        }
    }

    public void m15891a(WeightGoal weightGoal) {
        weightGoal.synced = WeightGoal.WEIGHTGOAL_NEED_SYNC;
        weightGoal.save();
    }

    public boolean m15892a(int i) {
        Object[] objArr = new Object[]{Integer.valueOf(WeightGoal.WEIGHTGOAL_NEED_DELETE)};
        List d = new Select().from(WeightGoal.class).where("FUID=?", Integer.valueOf(i)).and("Synced!=?", objArr).execute();
        if (d == null || d.size() == 0) {
            GuliLog.tracePrintInfo(f11511a, "the user fuid is first time to set weight goal");
            return true;
        }
        GuliLog.tracePrintInfo(f11511a, " the user fuid is not first time to set weight goal and the size is " + d.size());
        return false;
    }

    public void m15893b() {
        GuliLog.tracePrintWarn(f11511a, "Delete All Infos!!");
        new Delete().from(WeightGoal.class).execute();
    }

    public void deleteUserWeightGoalInLocal(int i) { // m15894b
        GuliLog.tracePrintInfo(f11511a, "deleteuserWeightGoalInLocal " + i);
        List<WeightGoal> d = new Select().from(WeightGoal.class).where("FUID=?", Integer.valueOf(i)).orderBy("DateTime DESC").execute();
        if (d == null || d.size() == 0) {
            GuliLog.tracePrintInfo(f11511a, "nothing goal found in fuid " + i);
            return;
        }
        ActiveAndroid.beginTransaction();
        try {
            for (WeightGoal weightGoal : d) {
                weightGoal.synced = WeightGoal.WEIGHTGOAL_NEED_DELETE;
                weightGoal.save();
            }
            ActiveAndroid.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ActiveAndroid.endTransaction();
        }
    }

//    public void m15895b(Context context) {
//        GuliLog.tracePrintInfo(f11511a, "clear all");
//        for (WeightGoal weightGoal : new Select().from(WeightGoal.class).execute()) {
//            GuliLog.tracePrintInfo(f11511a, "delete goal " + weightGoal.fuid + " " + weightGoal.datetime);
//            weightGoal.synced = WeightGoal.WEIGHTGOAL_NEED_DELETE;
//            weightGoal.save();
//            m15886b(context, weightGoal);
//        }
//    }

    public long m15896c(int i) {
        List d = new Select().from(WeightGoal.class).where("FUID=?", Integer.valueOf(i)).orderBy("DateTime DESC").execute();
        if (d == null || d.size() == 0) {
            GuliLog.tracePrintInfo(f11511a, "nothing goal found in fuid " + i);
            return -1;
        }
        WeightGoal weightGoal = (WeightGoal) d.get(0);
        GuliLog.tracePrintInfo(f11511a, "latest goal " + weightGoal.toString());
        return weightGoal.datetime;
    }
}
